//
//  SISSContractMutiSelectCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/10/9.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractMutiSelectCell.h"
#import "SISSTextView.h"

@interface SISSContractMutiSelectCell ()<UITextViewDelegate>

/* iconImageView */
@property (weak, nonatomic) UIImageView *iconImageView;
/* titleLabel */
@property (weak, nonatomic) UILabel *titleLabel;

/* buttonView */
@property (weak, nonatomic) UIView *buttonView;

/// textView
@property (weak, nonatomic) SISSTextView *textView;

/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;

@property (strong, nonatomic) NSMutableArray *arrM;

@end
@implementation SISSContractMutiSelectCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    self.arrM = [NSMutableArray arrayWithCapacity:self.contractModel.chooseArr.count + 1];
    for (NSInteger i = 0; i < self.contractModel.chooseArr.count + 1; i++) {
        [self.arrM addObject:@""];
    }
    
    self.titleLabel.text = contractModel.leftStr;
    
    self.isHiddenIconImage = contractModel.imageName.length == 0 || !contractModel.imageName;;
    self.isHiddenBottomTextView = contractModel.imageName.length != 0 || contractModel.imageName;
    self.iconImageView.image = [UIImage imageNamed:contractModel.imageName];
    self.textView.placeholder = contractModel.placeHolder;
    
    // 解决复用时按钮的个数
    for (UIButton *button in self.buttonView.subviews) {
        [button removeFromSuperview];
    }
   
    
    float butX = 15;
    float butY = 10;
    CGFloat leftTitleWidth = [contractModel.leftStr boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.width + 10;
    CGFloat currentWidth = SCREEN_WIDTH - leftTitleWidth;
    for(int i = 0; i < contractModel.chooseArr.count; i++){
        
        //宽度自适应
        NSDictionary *fontDict = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGRect frame_W = [contractModel.chooseArr[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDict context:nil];
        CGFloat buttonWidth;
        if (contractModel.isAutomaticWidth) {
             buttonWidth = frame_W.size.width + 30;
            if (butX + buttonWidth > currentWidth - 15) {
                butX = 15;
                butY += 55;
            }
           
        } else {
            buttonWidth = (currentWidth - 15 - 20 * 3) / 3;
            if (butX + buttonWidth + 20 > currentWidth - 15) {
                butX = 15;
                butY += 55;
            }
            
        }
        
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(butX, butY, buttonWidth, 40)];
        button.tag = 100 + i;
        [button setTitle:contractModel.chooseArr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor yyr_colorWithHexString:contractModel.colorHexStr] forState:UIControlStateNormal];
        [button setBackgroundImage:[ImageWithColor imageWithColor:[UIColor whiteColor]] forState: UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setBackgroundImage:[ImageWithColor imageWithColor:[UIColor yyr_colorWithHexString:contractModel.colorHexStr]] forState: UIControlStateSelected];
        [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        button.layer.cornerRadius = 20;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = [UIColor yyr_colorWithHexString:contractModel.colorHexStr].CGColor;
        button.layer.borderWidth = 1;
        [self.buttonView addSubview:button];
        
        butX = CGRectGetMaxX(button.frame) + 10;
        
        // 根据#把字符串拆分数组
        NSArray *arr = [contractModel.rightStr componentsSeparatedByString:@"#"];
        for (NSString *str in arr) {
            
            // 把选中的数据放入数组中对应的位置
            if ([str isEqualToString:contractModel.chooseArr[i]]) {
                [self.arrM replaceObjectAtIndex:i withObject:contractModel.chooseArr[i]];
            }
            
            if (![contractModel.chooseArr  containsObject:str]) {
                [self.arrM replaceObjectAtIndex:contractModel.chooseArr.count withObject:str];
            }
        }
        
        // 根据数据设置按钮的选中状态
        NSString *str = self.arrM[i];
        button.selected = str.length > 0;
    }
    
    self.textView.text = self.arrM.lastObject;
    [self.buttonView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(butY + 45);
    }];
}

- (void)buttonClickAction:(UIButton *)button {
    
    button.selected = !button.isSelected;
    
    NSString *tempStr = button.isSelected ? button.titleLabel.text : @"";
    
    [self.arrM replaceObjectAtIndex:button.tag - 100 withObject:tempStr];
    
    NSMutableArray *arrM = [NSMutableArray array];
    for (NSString *str in self.arrM) {
        if (str.length > 0) {
            [arrM addObject:str];
        }
    }
    
    self.contractModel.rightStr = [arrM componentsJoinedByString:@"#"];
    
    NSLog(@"== %@",[arrM componentsJoinedByString:@"#"]);
}

- (void)setIsHiddenIconImage:(BOOL)isHiddenIconImage {
    _isHiddenIconImage = isHiddenIconImage;
    self.iconImageView.hidden = isHiddenIconImage;
    CGFloat offset = isHiddenIconImage ? 0.01 : 24;
    WeakSelf
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.buttonView).offset(offset);
    }];
}

- (void)setIsHiddenBottomTextView:(BOOL)isHiddenBottomTextView {
    _isHiddenBottomTextView = isHiddenBottomTextView;
    self.textView.hidden = isHiddenBottomTextView;
    CGFloat offset = isHiddenBottomTextView ? 0.01 : 88;
    CGFloat topMargin = isHiddenBottomTextView ? 0.01 : 16;
    WeakSelf
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.buttonView.mas_bottom).offset(topMargin);
        make.height.mas_equalTo(offset);
    }];
}

#pragma mark - SISSTextViewDelegate
- (void)textViewDidEndEditing:(SISSTextView *)textView {
    
    [self.arrM replaceObjectAtIndex:self.arrM.count - 1 withObject:textView.text];
    
    NSMutableArray *arrM = [NSMutableArray array];
    for (NSString *str in self.arrM) {
        if (str.length > 0) {
            [arrM addObject:str];
        }
    }
    
    self.contractModel.rightStr = [arrM componentsJoinedByString:@"#"];
    
    NSLog(@"== %@",[arrM componentsJoinedByString:@"#"]);
}


#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UIImageView *iconImageView = [[UIImageView alloc] init];
    iconImageView.image = [UIImage imageNamed:@"设置"];
    [self.contentView addSubview:iconImageView];
    self.iconImageView = iconImageView;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = SECONDARY_TEXT_COLOR;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UIView *buttonView = [[UIView alloc] init];
    [self.contentView addSubview:buttonView];
    self.buttonView = buttonView;
    
    SISSTextView *textView = [[SISSTextView alloc] init];
    textView.font = [UIFont systemFontOfSize:16];
    textView.placeholder = @"请输入。。。。";
    textView.placeholderColor = SECONDARY_TEXT_COLOR;
    textView.delegate = self;
    textView.textColor = MAIN_TEXT_COLOR;
    textView.layer.borderWidth = 1;
    textView.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
//    textView.userInteractionEnabled = NO;
    [self.contentView addSubview:textView];
    self.textView = textView;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.titleLabel);
        make.bottom.equalTo(weakSelf.titleLabel.mas_top).offset(-margin);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(2 * margin);
        make.centerY.equalTo(weakSelf.buttonView).offset(3 * margin);
    }];
  
    [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(margin);
        make.left.equalTo(weakSelf.titleLabel.mas_right).offset(margin);
        make.right.equalTo(weakSelf.contentView).offset(-margin);
        make.height.mas_equalTo(100);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(2 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
        make.top.equalTo(weakSelf.buttonView.mas_bottom).offset(2 * margin);
        make.height.mas_equalTo(88);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.textView.mas_bottom).offset(margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}



@end
